#!/bin/csh -f
#_______________________________________________________________________________
#
# (C) dGB Beheer B.V.
# $Id$
#
# Sets up the OpendTect application and plugins
#_______________________________________________________________________________

set nonomatch=yes

set ask_dtect_appl=1

if ( "$1" == "--help" ) then
    echo "This script generates run scripts for OpendTect"
    echo "You can run this script at any time"
    echo "If you move the installation directory, you have to run this script"
    exit 0
endif

nextarg:

if ( "$1" == "--no_dtect_data" ) then
    set ask_dtect_appl=0
    shift; goto nextarg
endif

if ( ! -e ./mk_datadir ) then
    setenv instdir "$0:h"
    if ( -d "${instdir}" ) then
	cd "${instdir}"
    endif
    if ( ! -e ./mk_datadir ) then
	echo "Please run this script from the new d-Tect directory"
	exit 1
    endif
endif


#At this point currenct directory is installation directory.
setenv instdir `pwd`
set instdir="${instdir}"

#For the substitution by sed
setenv replaceinstdir `dirs -l| sed -e 's/\//\\\//g'`
set replaceinstdir="${replaceinstdir}"

if ( ! -w "${instdir}" ) then
    echo "Warning: cannot properly setup OpendTect."
    echo "Reason: ${instdir} is not writable."
    echo "Will exit the setup now, OpendTect is not set up propertly"
    exit 1
endif

if ( ! -w "${instdir}"/bin ) then
    echo "Warning: cannot properly setup OpendTect."
    echo "Reason: ${instdir}/bin is not writable."
    echo "Will exit the setup now, OpendTect is not set up propertly"
    exit 1
endif

set init_dtect_file="${instdir}"/bin/init_dtect
if ( -w "${init_dtect_file}" ) then
    sed -e "s/__INST_DIR__/${replaceinstdir}/" "${instdir}"/.init_dtect > "${instdir}"/bin/init_dtect
else
    if ( -e "${init_dtect_file}" ) then
	echo "Warning: cannot properly setup OpendTect."
	echo "Reason: ${instdir}/bin/init_dtect is not writable."
	echo "Will exit the setup now, OpendTect is not set up propertly"
	exit 1
    endif
endif

# this fetches the old DTECT_DATA
if ( -e "${init_dtect_file}" ) then
    source "${init_dtect_file}" >&! /dev/null
endif

#
# Get DTECT_DATA variable
#
if ( ! $?DTECT_DATA ) then
    if ( $?dGB_DATA ) then
	# relict of 'closed' times ....
	setenv DTECT_DATA $dGB_DATA
    endif

    if ( ! $?DTECT_DATA ) then
	if ( $ask_dtect_appl == 0 ) then
	    setenv DTECT_DATA ""
	else
	   echo ""
	   echo "The initial Project data directory (DTECT_DATA) can be forced."
	   echo "Such a directory must have been created by mk_datadir."
	   echo "If you don't already have one, you can make one later."
	   echo ""
	   echo "Forced initial value for DTECT_DATA (empty for none): "
	   echo -n ">> "
	   setenv DTECT_DATA "$<"
	endif
    endif
    if ( "$DTECT_DATA" != "" && ! -d "$DTECT_DATA" ) then
	set ddupdir="$DTECT_DATA":h
	if ( -d "$ddupdir" ) then
	    echo ""
	    echo -n \
		"Do you want to create the $DTECT_DATA directory [empty=n]? "
	    set ans=$<
	    if ( "$ans" == 'y' || "$ans" == 'Y' ) then
		./mk_datadir "$DTECT_DATA"
	    endif
	endif
    endif
endif

if ( "$DTECT_DATA" == "" || "$DTECT_DATA" == "/" ) then
    setenv replacedatadir __none__
else
    setenv replacedatadir `echo "$DTECT_DATA" | sed 's/\//\\\//g'`
endif

if ( "$DTECT_DATA" == "__DATA_DIR__" ) then
    setenv replacedatadir __none__
endif

#
# Generate Standard scripts
#

foreach fil ( init_dtect start_dtect init_dtect_user exec_prog )
    sed -e "s/__INST_DIR__/$replaceinstdir/" \
	-e "s/__DATA_DIR__/$replacedatadir/" \
	"${instdir}"/.$fil > "${instdir}"/bin/$fil
end
chmod +x "${instdir}"/bin/{start_dtect,init_dtect_user,exec_prog}
#Recreating links to current OpendTect installation
ln -sf "${instdir}"/bin/{start_dtect,exec_prog} "${instdir}"

#
# Generate scripts for added plugins
#

foreach fil ( .install.*.license .start.*.lmgrd )
    if ( ! -r "${instdir}"/$fil ) continue
    set outfil=`echo $fil | sed 's/^\.//'`
    sed -e "s/__INST_DIR__/$replaceinstdir/" \
	-e "s/__DATA_DIR__/$replacedatadir/" \
	"${instdir}"/$fil > "${instdir}"/$outfil
    chmod +x "${instdir}"/$outfil
end

# make sure the right stuff is set for the external scripts
source "${init_dtect_file}" >&! /dev/null

foreach fil ( setup.od.* )
    if ( -e "${instdir}"/$fil ) then
	chmod +x "${instdir}"/$fil
	"${instdir}"/$fil
    endif
end


exit 0

